package com.sisyphus.spinach.network.views;

import android.view.LayoutInflater;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sisyphus.spinach.core.INetPageTask;
import com.sisyphus.spinach.core.INetTask;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 分页
 */
public class SmartRefreshList<T> implements INetPageTask.View<T> {

    private final BaseQuickAdapter<T, BaseViewHolder> mAdapter;

    private final INetTask.View<T> mStatusView;

    private final RecyclerView mRecyclerView;

    private final SmartRefreshLayout mSmartRefreshLayout;

    private int mEmptyLayoutResId, mHeaderLayoutResId, mNotMoreFooterLayoutResId;

    public SmartRefreshList(@NonNull RecyclerView recyclerView, @NonNull SmartRefreshLayout refreshLayout) {
        this(recyclerView, refreshLayout, null);
    }

    public SmartRefreshList(@NonNull RecyclerView recyclerView, @NonNull SmartRefreshLayout refreshLayout, INetTask.View<T> view) {
        mStatusView = view;
        mRecyclerView = recyclerView;
        mSmartRefreshLayout = refreshLayout;

        // 类型检查
        if (mRecyclerView.getAdapter() instanceof BaseQuickAdapter) {
            mAdapter = (BaseQuickAdapter<T, BaseViewHolder>) mRecyclerView.getAdapter();

        } else {
            throw new IllegalStateException("RecyclerView.Adapter 必须实现 BaseQuickAdapter");
        }

        // 观察数据改变
        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

            @Override
            public void onChanged() {
                // 无数据时禁止上拉加载更多
                if (mAdapter.getData().isEmpty()) {
                    mSmartRefreshLayout.setEnableLoadMore(false);
                }
            }

        });
    }

    @Override
    public void renderRefresh(List<T> refreshData) {
        // 加载数据
        mAdapter.setList(refreshData);

        // 初次加载数据时添加头部
        if (mHeaderLayoutResId != 0 && mAdapter.getItemCount() <= 0) {
            mAdapter.addHeaderView(LayoutInflater.from(mRecyclerView.getContext())
                    .inflate(mHeaderLayoutResId, mRecyclerView, false));
        }

        // 移除尾部没有更多数据布局，如果存在
        if (mAdapter.getFooterLayoutCount() > 0) {
            mAdapter.removeAllFooterView();
        }

        // 延迟添加空布局，避免首次加载就呈现空布局
        if (!mAdapter.hasEmptyView() && mEmptyLayoutResId != 0) {
            mAdapter.setEmptyView(mEmptyLayoutResId);
        }

        // 可以加载更多
        mSmartRefreshLayout.setEnableLoadMore(true);
    }

    @Override
    public void renderLoadMore(List<T> loadMoreData) {
        mAdapter.addData(loadMoreData);
    }

    @Override
    public void renderNoMore() {
        // 关闭可以上拉加载数据
        mSmartRefreshLayout.setEnableLoadMore(false);

        // 判断加入没有更多数据布局
        if (mAdapter.getFooterLayoutCount() < 1 && mNotMoreFooterLayoutResId != 0) {
            mAdapter.addFooterView(LayoutInflater.from(mRecyclerView.getContext()).inflate(
                    mNotMoreFooterLayoutResId, mRecyclerView, false));
        }
    }

    @Override
    public void start() {
        if (mAdapter.getData().isEmpty() && mStatusView != null) {
            mStatusView.start();
        }
    }

    @Override
    public void empty(CharSequence charSequence) {
        if (mEmptyLayoutResId != 0) {
            mAdapter.setList(null);
        }
    }

    @Override
    public void failure(Throwable throwable) {
        if (!mAdapter.getData().isEmpty()) {
            if (mStatusView != null) {
                mStatusView.failure(throwable);
            }

            mSmartRefreshLayout.setEnableLoadMore(true);
        }
    }

    @Override
    public void complete() {
        if (mStatusView != null) {
            mStatusView.complete();
        }

        // 关闭动画
        mSmartRefreshLayout.finishRefresh();
        mSmartRefreshLayout.finishLoadMore();
    }

    public SmartRefreshList<T> setEmptyLayoutResId(int emptyLayoutResId) {
        this.mEmptyLayoutResId = emptyLayoutResId;
        return this;
    }

    public SmartRefreshList<T> setNotMoreFooterLayoutResId(int notMoreFooterLayoutResId) {
        this.mNotMoreFooterLayoutResId = notMoreFooterLayoutResId;
        return this;
    }

    public SmartRefreshList<T> addHeaderLayoutResId(int headerLayoutResId) {
        this.mHeaderLayoutResId = headerLayoutResId;
        return this;
    }

}
